define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'general/screens/index',
                    add_url: 'general/screens/add',
                    edit_url: 'general/screens/edit',
                    del_url: 'general/screens/del',
                    multi_url: 'general/screens/multi',
                    table: 'screens',
                }
            });

            var table = $("#table");
            var regionList = Config.regionList;
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'tag_id', title: __('Tag_id'),searchList:regionList,formatter:function(value,row,index){
                            if(row.tag){
                                return row.tag.name;
                            }else{
                                return '-';
                            }
                        }},
                        {field: 'name', title: __('Name'),operate: 'LIKE %...%'},
                        {field: 'hsize', title: __('Hsize')},
                        {field: 'vsize', title: __('Vsize')},
                        /*{field: 'userid', title: __('Userid')},*/
                        {field: 'private', title: __('展示'),align: 'center',formatter: Table.api.formatter.toggle, searchList: {"1":__('展示'),"0":__('不展示')}},
                        {field: 'updatetime', title: __('Update time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                        buttons: [
                                {
                                    name: 'detail',
                                    text: __('预览屏幕'),
                                    title: __('预览屏幕'),
                                    classname: 'btn btn-xs btn-info btn-drawing',
                                    icon: 'fa fa-folder-o',
                                    // url: '/screen/index?id=',
                                    visible:function(row,full){
                                         full.classname = 'btn btn-xs btn-info btn-drawing id_'+row.id;
                                         return true;
                                    }
                                }]
                      }
                    ]
                ]
            });

            $(document).on("click", ".btn-drawing", function () {
                var id_str = $(this).attr('class');
                var slice_str = id_str.split(' ')[4];
                var ids=slice_str.slice(3);
                window.open("/screen/drawing?id="+ids);  
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});